package com.lg.sysfile.service.impl;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lg.config.BootLgConfig;
import com.lg.sysfile.dao.SysFileDao;
import com.lg.sysfile.entity.SysFile;
import com.lg.sysfile.service.SysFileService;
import com.lg.utils.FileUtil;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dl
 * @since 2018-04-08
 */
@Transactional
@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileDao, SysFile> implements SysFileService {

	@Autowired
	BootLgConfig bootLgConfig;
	
	public boolean save( SysFile sysFile) {
		sysFile.setGmtCreate(new Date());
		return super.insert(sysFile);
	}

	@Override
	public boolean update(SysFile sysFile) {
		sysFile.setGmtModified(new Date());
		return super.updateById(sysFile);
	}
	
	
	@Override
	public boolean deleteById(Long id) {
		return super.deleteById(id);
	}

	@Override
	public boolean deleteByIds(List<Long> ids) {
		if(null==ids||ids.isEmpty()){
			return false;
		}
		return super.deleteBatchIds(ids);
	}

	@Override
	public SysFile upload(MultipartFile files) throws IOException {
		String finalPath[]=FileUtil.uploadFile(files.getBytes(), bootLgConfig.getUploadPath(), FileUtil.renameToUUID(files.getOriginalFilename()));
		SysFile sysFile=new SysFile();
		sysFile.setFileName(files.getOriginalFilename());
		sysFile.setFileType(FileUtil.getPrefix(files.getOriginalFilename()));
		sysFile.setFileSize(files.getSize());
		sysFile.setGmtCreate(new Date());
		sysFile.setLocalUrl(bootLgConfig.getPathPatterns()+finalPath[1]);
		save(sysFile);
		return sysFile;
	}

}
